πŸ“Ÿ 6. μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”

@Seongukbaek Posted July 23, 2022 Β· Updated July 23, 2022 Β· 14 min read

μŠ€ν”„λ§ 핡심 원리 - κΈ°λ³ΈνŽΈμ„ λ“€μœΌλ©° μ •λ¦¬ν•˜λŠ” POSTμž…λ‹ˆλ‹€.

🎯 μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”κ³Ό μ˜μ‘΄κ΄€κ³„ μžλ™ μ£Όμž… μ‹œμž‘ν•˜κΈ°

μ§€κΈˆκΉŒμ§€λŠ” μŠ€ν”„λ§ 빈 등둝 μ‹œ, μžλ°” μ½”λ“œμ˜ @Bean μ΄λ‚˜ XML의 <bean></bean> 을 톡해 μ„€μ • 정보에 직접 등둝할 μŠ€ν”„λ§ λΉˆμ„ λͺ…μ‹œν–ˆλ‹€.

  • ν•˜μ§€λ§Œ 등둝해야 ν•  μŠ€ν”„λ§ 빈의 μˆ˜κ°€ λ§Žμ•„μ§€κ²Œ 되면, μ΄λŸ¬ν•œ 과정은 맀우 λΉ„νš¨μœ¨μ μ΄λ‹€.

μŠ€ν”„λ§μ€ μ„€μ • 정보가 없어도 μžλ™μœΌλ‘œ μŠ€ν”„λ§ λΉˆμ„ λ“±λ‘ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”μ΄λΌλŠ” κΈ°λŠ₯을 μ œκ³΅ν•œλ‹€!

  • λ˜ν•œ μ˜μ‘΄κ΄€κ³„λ„ μžλ™μœΌλ‘œ μ£Όμž…ν•˜λŠ” @Autowired λΌλŠ” κΈ°λŠ₯도 μ œκ³΅ν•œλ‹€.

λ¨Όμ € μ½”λ“œλ‘œ μ•Œμ•„λ³΄κΈ° μœ„ν•΄, μƒˆλ‘œμš΄ AutoAppConfig.java λ₯Ό μƒμ„±ν•œλ‹€.

package hello.core; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; @Configuration @ComponentScan( excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class) ) public class AutoAppConfig { }
  • @ComponentScan 이 μΆ”κ°€λ˜μ—ˆλ‹€. μ΄λŠ” μŠ€ν”„λ§ λΉˆμ„ λ‹€ 읽어 μžλ™μœΌλ‘œ λ“±λ‘ν•˜κΈ° μœ„ν•΄ ν•„μš”ν•œ μ–΄λ…Έν…Œμ΄μ…˜μ΄λ‹€.

    • μ΄λŠ” @Component μ–΄λ…Έν…Œμ΄μ…˜μ΄ 뢙은 클래슀λ₯Ό μ°Ύμ•„ μžλ™μœΌλ‘œ μŠ€ν”„λ§ 빈으둜 등둝해쀀닀.
    • excludeFilters λŠ” μ΄λ•Œ μ œμ™Έν•  클래슀λ₯Ό μ˜λ―Έν•œλ‹€.
    • ν˜„μž¬ μ˜ˆμ œμ—μ„œλŠ” 이전에 μƒμ„±ν–ˆλ˜ AppConfig 에 @Configuration μ–΄λ…Έν…Œμ΄μ…˜μ΄ λΆ™μ–΄μžˆκ³ , 이λ₯Ό μ‚­μ œν•˜μ§€ μ•ŠκΈ°μ— 이λ₯Ό μ œμ™Έν•œλ‹€.
  • 기쑴의 AppConfig 와 λ‹€λ₯΄κ²Œ @Bean 으둜 λ“±λ‘ν•œ ν΄λž˜μŠ€κ°€ ν•˜λ‚˜λ„ μ—†λ‹€!

이제 각 ν΄λž˜μŠ€κ°€ μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”μ˜ λŒ€μƒμ΄ λ˜λ„λ‘ @Component μ–΄λ…Έν…Œμ΄μ…˜μ„ λΆ™μ—¬μ€€λ‹€.

MemoryMemberRepository @Component μΆ”κ°€, RateDiscountPolicy @Component μΆ”κ°€

MemberServiceImpl @Component, @Autowired μΆ”κ°€

package hello.core.member; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MemberServiceImpl implements MemberService { private final MemberRepository memberRepository; @Autowired public MemberServiceImpl(MemberRepository memberRepository) { this.memberRepository = memberRepository; } @Override public void join(Member member) { memberRepository.save(member); } @Override public Member findMember(Long memberId) { return memberRepository.findById(memberId); } }
  • 이전 AppConfig μ—μ„œλŠ” @Bean 으둜 직접 μ„€μ • 정보λ₯Ό μž‘μ„±ν–ˆκ³ , μ˜μ‘΄κ΄€κ³„λ„ 직접 λͺ…μ‹œν–ˆλ‹€.
  • ν•˜μ§€λ§Œ μ΄μ œλŠ” μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”μœΌλ‘œ μžλ™ λ“±λ‘ν•˜κΈ°μ— μ˜μ‘΄κ΄€κ³„ μ£Όμž… λ˜ν•œ ν•΄λ‹Ή 클래슀 λ‚΄μ—μ„œ μ²˜λ¦¬ν•΄μ•Ό ν•œλ‹€.

    • μ΄λ•Œ μ‚¬μš©ν•˜λŠ” 것이 @Autowired μ–΄λ…Έν…Œμ΄μ…˜μ΄λ‹€. μ΄λŠ” μ˜μ‘΄κ΄€κ³„λ₯Ό μžλ™μœΌλ‘œ μ£Όμž…ν•΄μ€€λ‹€.

OrderServiceImpl @Component, @Autowired μΆ”κ°€

package hello.core.order; import hello.core.discount.DiscountPolicy; import hello.core.discount.FixDiscountPolicy; import hello.core.discount.RateDiscountPolicy; import hello.core.member.Member; import hello.core.member.MemberRepository; import hello.core.member.MemoryMemberRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class OrderServiceImpl implements OrderService { // νšŒμ›μ„ μ°ΎκΈ° μœ„ν•΄ ν•„μš” private final MemberRepository memberRepository; // 할인 μ •μ±… μ‚¬μš©μ„ μœ„ν•΄ ν•„μš” // private final DiscountPolicy discountPolicy = new FixDiscountPolicy(); private final DiscountPolicy discountPolicy; @Autowired public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy) { this.memberRepository = memberRepository; this.discountPolicy = discountPolicy; } @Override public Order createOrder(Long memberId, String itemName, int itemPrice) { Member member = memberRepository.findById(memberId); // 할인에 λŒ€ν•΄μ„œλŠ” createOrderλŠ” μ•„μ˜ˆ μ•Œμ§€ λͺ»ν•¨, 단일 μ±…μž„ 원칙을 잘 지킨 예 int discountPrice = discountPolicy.discount(member, itemPrice); return new Order(memberId, itemName, itemPrice, discountPrice); } }
  • @Autowired λ₯Ό μ‚¬μš©ν•˜λ©΄ μƒμ„±μžμ—μ„œ μ—¬λŸ¬ μ˜μ‘΄κ΄€κ³„λ„ ν•œ λ²ˆμ— μ£Όμž…λ°›μ„ 수 μžˆλ‹€.

이제 μƒμ„±ν•œ μ„€μ • μ •λ³΄λ‘œ μŠ€ν”„λ§ 빈 등둝이 정상 μˆ˜ν–‰λ˜λŠ”μ§€ ν™•μΈν•˜λŠ” ν…ŒμŠ€νŠΈ μ½”λ“œλ₯Ό μž‘μ„±ν•œλ‹€.

AutoAppConfigTest.java

package hello.core.scan; import hello.core.AutoAppConfig; import hello.core.member.MemberService; import hello.core.member.MemberServiceImpl; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import static org.assertj.core.api.Assertions.*; public class AutoAppConfigTest { @Test void basicScan() { ApplicationContext ac = new AnnotationConfigApplicationContext(AutoAppConfig.class); MemberService memberService = ac.getBean(MemberService.class); assertThat(memberService).isInstanceOf(MemberService.class); } }
  • μ„€μ • μ •λ³΄λ‘œ 방금 μƒμ„±ν•œ AutoAppConfig λ₯Ό λ„˜κ²¨μ€€λ‹€.

πŸͺ” μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”κ³Ό μžλ™ μ˜μ‘΄κ΄€κ³„ μ£Όμž… λ™μž‘ κ³Όμ •

  1. @ComponentScan
  2. @ComponentScan 은 @Component κ°€ 뢙은 λͺ¨λ“  클래슀λ₯Ό μŠ€ν”„λ§ 빈으둜 λ“±λ‘ν•œλ‹€. (μŠ€ν”„λ§ μ»¨ν…Œμ΄λ„ˆκ°€ λͺ¨λ“  클래슀λ₯Ό 확인!, μ‹±κΈ€ν†€μœΌλ‘œ 등둝)
  3. μ΄λ•Œ μŠ€ν”„λ§ 빈의 κΈ°λ³Έ 이름은 클래슀λͺ…을 μ‚¬μš©ν•˜λ˜, 맨 μ•žκΈ€μžλ§Œ μ†Œλ¬Έμžλ₯Ό μ‚¬μš©ν•œλ‹€.

    • λ§Œμ•½ 빈 이름을 μ§€μ •ν•˜κ³  μ‹ΆμœΌλ©΄ @Component("memberService2") 와 같이 λΆ€μ—¬ν•˜λ©΄ λœλ‹€.
  4. @Autowired μ˜μ‘΄κ΄€κ³„ μžλ™ μ£Όμž…
  5. μƒμ„±μžμ— @Autowired λ₯Ό μ§€μ •ν•˜λ©΄, μŠ€ν”„λ§ μ»¨ν…Œμ΄λ„ˆκ°€ μžλ™μœΌλ‘œ ν•΄λ‹Ή μŠ€ν”„λ§ λΉˆμ„ μ°Ύμ•„μ„œ μ£Όμž…ν•œλ‹€.
  6. μ΄λ•Œ κΈ°λ³Έ 쑰회 μ „λž΅μ€ νƒ€μž…μ΄ 같은 λΉˆμ„ μ°Ύμ•„ μ£Όμž…ν•œλ‹€.

    • getBean(MemberRepository.class) 와 λ™μΌν•˜λ‹€κ³  μ΄ν•΄ν•˜λ©΄ 쉽닀.
    • νƒ€μž…μ΄ 같은 빈이 μ—¬λŸ¬ 개인 κ²½μš°λŠ” λ’€μ—μ„œ ...
  • μƒμ„±μžμ— νŒŒλΌλ―Έν„°κ°€ λ§Žμ•„λ„, λ‹€ μ°Ύμ•„μ„œ μžλ™μœΌλ‘œ μ£Όμž…ν•œλ‹€.

🎯 탐색 μœ„μΉ˜μ™€ κΈ°λ³Έ μŠ€μΊ” λŒ€μƒ

πŸͺ” 탐색할 νŒ¨ν‚€μ§€μ˜ μ‹œμž‘ μœ„μΉ˜ μ§€μ •

μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ” μ‹œ, λͺ¨λ“  μžλ°” 클래슀λ₯Ό ν™•μΈν•˜λ©΄ μ‹œκ°„μ΄ 였래 κ±Έλ¦°λ‹€. κ·Έλž˜μ„œ ν•„μš”ν•œ μœ„μΉ˜λΆ€ν„° νƒμƒ‰ν•˜λ„λ‘ μ‹œμž‘ μœ„μΉ˜λ₯Ό μ§€μ •ν•  수 μžˆλ‹€.

package hello.core; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; @Configuration @ComponentScan( excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class), basePackages = "hello.core" ) public class AutoAppConfig { }
  • basePackages : 탐색할 νŒ¨ν‚€μ§€μ˜ μ‹œμž‘ μœ„μΉ˜λ₯Ό μ§€μ •ν•œλ‹€. 이 νŒ¨ν‚€μ§€λ₯Ό ν¬ν•¨ν•œ ν•˜μœ„ νŒ¨ν‚€μ§€λ₯Ό λͺ¨λ‘ νƒμƒ‰ν•œλ‹€. μ—¬λŸ¬ μ‹œμž‘ μœ„μΉ˜λ„ μ§€μ • κ°€λŠ₯ν•˜λ‹€.
  • basePackageClasses : μ§€μ •ν•œ 클래슀의 νŒ¨ν‚€μ§€λ₯Ό 탐색 μ‹œμž‘ μœ„μΉ˜λ‘œ μ§€μ •ν•œλ‹€.
  • μ§€μ •ν•˜μ§€ μ•ŠλŠ”λ‹€λ©΄, @ComponentScan 이 뢙은 μ„€μ • 정보 클래슀의 νŒ¨ν‚€μ§€κ°€ μ‹œμž‘ μœ„μΉ˜κ°€ λœλ‹€.

ꢌμž₯ν•˜λŠ” 방법

  • νŒ¨ν‚€μ§€ μœ„μΉ˜λ₯Ό μ§€μ •ν•˜μ§€ μ•Šκ³ , μ„€μ • 정보 클래슀의 μœ„μΉ˜λ₯Ό ν”„λ‘œμ νŠΈ μ΅œμƒλ‹¨μ— λ‘λŠ” 것이닀.
  • 졜근 μŠ€ν”„λ§ λΆ€νŠΈλ„ 이 방법을 기본으둜 μ œκ³΅ν•œλ‹€.

    • μŠ€ν”„λ§ λΆ€νŠΈλ₯Ό μ‚¬μš©ν•˜λ©΄ μŠ€ν”„λ§ λΆ€νŠΈμ˜ λŒ€ν‘œ μ‹œμž‘ 정보인 @SpringBootApplication μ–΄λ…Έν…Œμ΄μ…˜μ„ ν”„λ‘œμ νŠΈ μ‹œμž‘ 루트 μœ„μΉ˜μ— λ‘λŠ” 것이 관둀이고, 이 μ„€μ • μ•ˆμ— @ComponentScan μ–΄λ…Έν…Œμ΄μ…˜μ΄ λ“€μ–΄μžˆλ‹€.

πŸͺ” μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ” κΈ°λ³Έ λŒ€μƒ

μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”μ€ λ‹€μŒκ³Ό 같은 μ–΄λ…Έν…Œμ΄μ…˜λ„ μΆ”κ°€λ‘œ λŒ€μƒμ— ν¬ν•¨ν•œλ‹€.

  • @Component : μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”μ—μ„œ μ‚¬μš©
  • @Controller : μŠ€ν”„λ§ MVC μ»¨νŠΈλ‘€λŸ¬μ—μ„œ μ‚¬μš©
  • @Service : μŠ€ν”„λ§ λΉ„μ¦ˆλ‹ˆμŠ€ λ‘œμ§μ—μ„œ μ‚¬μš©
  • @Repository : μŠ€ν”„λ§ 데이터 μ ‘κ·Ό κ³„μΈ΅μ—μ„œ μ‚¬μš©
  • @Configuration : μŠ€ν”„λ§ μ„€μ • μ •λ³΄μ—μ„œ μ‚¬μš©

μœ„ 클래슀의 μ†ŒμŠ€ μ½”λ“œλ₯Ό 보면 λͺ¨λ‘ @Component λ₯Ό ν¬ν•¨ν•˜κ³  μžˆλŠ” 것을 확인할 수 μžˆλ‹€.

사싀 μ–΄λ…Έν…Œμ΄μ…˜μ€ 상속 κ΄€κ³„λΌλŠ” 것이 μ—†λ‹€. κ·Έλž˜μ„œ μ–΄λ…Έν…Œμ΄μ…˜μ΄ νŠΉμ • μ–΄λ…Έν…Œμ΄μ…˜μ„ λ“€κ³  μžˆλŠ” 것을 μΈμ‹ν•˜λŠ” 것은 μžλ°” 제곡 κΈ°λŠ₯이 μ•„λ‹Œ, μŠ€ν”„λ§ 제곡 κΈ°λŠ₯이닀.

μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”μ˜ μš©λ„ 뿐 μ•„λ‹ˆλΌ λ‹€μŒ μ–΄λ…Έν…Œμ΄μ…˜μ΄ μžˆλ‹€λ©΄ μŠ€ν”„λ§μ€ λΆ€κ°€ κΈ°λŠ₯을 μˆ˜ν–‰ν•œλ‹€.

  • @Controller : μŠ€ν”„λ§ MVC 컨트둀러둜 인식
  • @Repository : μŠ€ν”„λ§ 데이터 μ ‘κ·Ό κ³„μΈ΅μœΌλ‘œ μΈμ‹ν•˜κ³ , 데이터 κ³„μΈ΅μ˜ μ˜ˆμ™Έλ₯Ό μŠ€ν”„λ§ μ˜ˆμ™Έλ‘œ λ³€ν™˜ν•΄μ€€λ‹€.

    • DBκ°€ λ³€κ²½λ˜μ—ˆμ„ λ•Œ, μ˜ˆμ™Έλ„ λ³€κ²½λ˜μ–΄ λ‹€λ₯Έ κ³„μΈ΅μ˜ μ½”λ“œ λ˜ν•œ λ³€κ²½λ˜μ–΄μ•Ό ν•˜λŠ” 문제λ₯Ό λ°©μ§€ν•œλ‹€.
  • @Configuration : μŠ€ν”„λ§ μ„€μ • μ •λ³΄λ‘œ μΈμ‹ν•˜κ³ , μŠ€ν”„λ§ 빈이 싱글톀을 μœ μ§€ν•˜λ„λ‘ μΆ”κ°€ 처리λ₯Ό ν•œλ‹€.
  • @Service : νŠΉλ³„ν•œ 처리λ₯Ό ν•˜μ§€ μ•ŠλŠ”λ‹€. λΉ„μ¦ˆλ‹ˆμŠ€ 계측을 κ°œλ°œμžλ“€μ΄ μΈμ‹ν•˜λŠ”λ° 도움을 μ€€λ‹€.

useDefaultFilters μ˜΅μ…˜μ€ 기본으둜 μΌœμ Έμžˆλ‹€. 이 μ˜΅μ…˜μ„ 끄면 κΈ°λ³Έ μŠ€μΊ” λŒ€μƒλ“€μ΄ μ œμ™Έλœλ‹€.


🎯 ν•„ν„°

  • includeFilters : μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ” λŒ€μƒμ„ μΆ”κ°€λ‘œ μ§€μ •ν•œλ‹€.
  • excludeFilters : μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”μ—μ„œ μ œμ™Έν•  λŒ€μƒμ„ μ§€μ •ν•œλ‹€.

μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ” λŒ€μƒμ— μΆ”κ°€ν•  μ–΄λ…Έν…Œμ΄μ…˜

package hello.core.scan.filter; import java.lang.annotation.*; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface MyIncludeComponent { } // package hello.core.scan.filter; @MyIncludeComponent public class BeanA { }
  • TYPE : 클래슀 λ ˆλ²¨μ— λΆ™λŠ” μ–΄λ…Έν…Œμ΄μ…˜
  • MyIncludeComponent μ–΄λ…Έν…Œμ΄μ…˜μ΄ 뢙은 클래슀(μœ„μ—μ„œλŠ” BeanA)λŠ” μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”μ— μΆ”κ°€ν•  것이닀!!!

μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ” λŒ€μƒμ—μ„œ μ œμ™Έν•  μ–΄λ…Έν…Œμ΄μ…˜

package hello.core.scan.filter; import java.lang.annotation.*; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface MyExcludeComponent { } // package hello.core.scan.filter; @MyExcludeComponent public class BeanB { }
  • MyExcludeComponent μ–΄λ…Έν…Œμ΄μ…˜μ΄ 뢙은 클래슀(μœ„μ—μ„œλŠ” BeanB)λŠ” μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”μ—μ„œ μ œμ™Έν•  것이닀!!!

μ„€μ • 정보와 ν…ŒμŠ€νŠΈ μ½”λ“œ

package hello.core.scan.filter; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import static org.assertj.core.api.Assertions.*; import static org.springframework.context.annotation.ComponentScan.*; public class ComponentFilterAppConfigTest { @Test void filterScan() { ApplicationContext ac = new AnnotationConfigApplicationContext(ComponentFilterAppConfig.class); BeanA beanA = ac.getBean("beanA", BeanA.class); assertThat(beanA).isNotNull(); Assertions.assertThrows( NoSuchBeanDefinitionException.class, () -> ac.getBean("beanB", BeanB.class) ); } @Configuration @ComponentScan( includeFilters = @Filter(type = FilterType.ANNOTATION, classes = MyIncludeComponent.class), excludeFilters = @Filter(type = FilterType.ANNOTATION, classes = MyExcludeComponent.class) ) static class ComponentFilterAppConfig { } }
  • ComponentFilterAppConfig μ„€μ • μ •λ³΄λŠ” μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”μ„ μˆ˜ν–‰ν•˜λŠ”λ°, MyIncludeComponent μ–΄λ…Έν…Œμ΄μ…˜μ΄ 뢙은 ν΄λž˜μŠ€λŠ” μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ” λŒ€μƒμ— ν¬ν•¨ν•˜κ³ , MyExcludeComponent μ–΄λ…Έν…Œμ΄μ…˜μ΄ 뢙은 ν΄λž˜μŠ€λŠ” ν¬ν•¨ν•˜μ§€ μ•ŠλŠ”λ‹€.

πŸͺ” FilterType μ˜΅μ…˜

5κ°€μ§€ μ˜΅μ…˜μ΄ μžˆλ‹€.

  • ANNOTATION : κΈ°λ³Έκ°’, μ–΄λ…Έν…Œμ΄μ…˜μ„ μΈμ‹ν•΄μ„œ λ™μž‘ν•œλ‹€. (μƒλž΅ κ°€λŠ₯)

    • ex. org.example.SomeAnnotation
  • ASSIGNABLE_TYPE : μ§€μ •ν•œ νƒ€μž…κ³Ό μžμ‹ νƒ€μž…μ„ μΈμ‹ν•΄μ„œ λ™μž‘ν•œλ‹€.

    • ex. org.example.SomeClass
  • ASPECTJ : AspectJ νŒ¨ν„΄ μ‚¬μš©

    • ex. org.example..*Service+
  • REGEX : μ •κ·œ ν‘œν˜„μ‹

    • ex. org\.example\.Default.*
  • CUSTOM : TypeFilter λΌλŠ” μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•΄μ„œ 처리

    • ex. org.example.MyTypeFilter

예λ₯Ό λ“€μ–΄ BeanA ν΄λž˜μŠ€λ„ μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”μ—μ„œ μ œμ™Έν•˜κ³  μ‹Άλ‹€λ©΄,

@Configuration @ComponentScan( includeFilters = @Filter(type = FilterType.ANNOTATION, classes = MyIncludeComponent.class), excludeFilters = { @Filter(type = FilterType.ANNOTATION, classes = MyExcludeComponent.class) @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = BeanA.class) } )

includeFilters λ₯Ό 거의 μ‚¬μš©ν•  일은 μ—†λ‹€! @Component λ©΄ μΆ©λΆ„ν•˜λ‹€...


🎯 쀑볡 등둝과 좩돌

μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”μ—μ„œ 같은 빈 이름을 λ“±λ‘ν•˜λ©΄ μ–΄λ–»κ²Œ 될까? λ‹€μŒκ³Ό 같은 두 κ°€μ§€ 상황이 μžˆλ‹€.

  • μžλ™ 빈 등둝 vs. μžλ™ 빈 등둝
  • μˆ˜λ™ 빈 등둝 vs. μžλ™ 빈 등둝

πŸͺ” μžλ™ 빈 등둝 vs. μžλ™ 빈 등둝

μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”μ— μ˜ν•΄ μžλ™μœΌλ‘œ μŠ€ν”„λ§ 빈이 λ“±λ‘λ˜λŠ”λ°, κ·Έ 이름이 같은 경우 μŠ€ν”„λ§μ€ 였λ₯˜λ₯Ό λ°œμƒμ‹œν‚¨λ‹€.

  • ConflictingBeanDefinitionException μ˜ˆμ™Έ λ°œμƒ!

πŸͺ” μˆ˜λ™ 빈 등둝 vs. μžλ™ 빈 등둝

@Component public class MemoryMemberRepository implements MemberRepository {}
  • μžλ™ 빈 등둝
@Configuration @ComponentScan( excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class), basePackages = "hello.core" ) public class AutoAppConfig { // 빈 등둝 μ‹œ, 맨 μ•ž κΈ€μžλŠ” μ†Œλ¬Έμžλ‘œ! @Bean(name = "memoryMemberRepository") public MemberRepository memberRepository() { return new MemoryMemberRepository(); } }
  • μˆ˜λ™ 빈 등둝
  • 그리고 μ΄λ ‡κ²Œ μˆ˜λ™ 빈 등둝과 μžλ™ 빈 등둝이 μΆ©λŒλ˜λŠ” 경우, μˆ˜λ™ 빈 등둝이 μš°μ„ κΆŒμ„ κ°€μ Έ μˆ˜λ™ 빈이 μžλ™ λΉˆμ„ μ˜€λ²„λΌμ΄λ”©ν•΄λ²„λ¦°λ‹€.

μˆ˜λ™ 빈 등둝 μ‹œ λ‚¨λŠ” 둜그

Overriding bean definition for bean 'memoryMemberRepository' with a different definition: replacing
  • 졜근 μŠ€ν”„λ§ λΆ€νŠΈμ—μ„œλŠ” μˆ˜λ™ 빈 등둝과 μžλ™ 빈 등둝이 μΆ©λŒλ‚˜λ©΄, 였λ₯˜κ°€ λ°œμƒν•˜λ„λ‘ κΈ°λ³Έ 값을 λ°”κΏ¨λ‹€. (μŠ€ν”„λ§ λΆ€νŠΈμΈ CoreApplication 을 싀행해보면 였λ₯˜λ₯Ό 확인할 수 μžˆλ‹€.)

항상 작기 μ–΄λ €μš΄ λ²„κ·ΈλŠ” μ• λ§€ν•œ 버그닀!

πŸ“Œ μ€‘μš”ν•œ κ°œλ…

μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”, μ–΄λ…Έν…Œμ΄μ…˜

πŸ“• μ°Έκ³ 

@Seongukbaek
배우고 μ •λ¦¬ν•˜κ³  κ³΅μœ ν•˜λŠ” λŒ€ν•™μƒ λ°±μ„±μš±μž…λ‹ˆλ‹€ πŸ˜€